﻿using System.Collections.ObjectModel;

namespace PMIS.Shared.DTO
{
    public class MenuDto: ModelBase
    {
        public string? Id { get; set; } 

        public string? ParentId { get; set; }

        public string? ParentName { get; set; }

        public string Name { get; set; }

        public string? Icon { get; set; } 

        public string? Url { get; set; }

        public int? Sort { get; set; } = 1;

        public int? Type { get; set; } = 1;

        public string? TypeName { get; set; }

        public int? Status { get; set; } = 1;

        public string? AuthorizeCode { get; set; }

        public string? Remark { get; set; }

        private List<MenuDto>? _children;
        public List<MenuDto>? Children 
        { 
            get
            {
                return _children;
            }
            set
            {
                SetProperty(ref _children, value);
            }
        }

        private bool _isExpanded;
        public bool IsExpanded
        {
            get
            {
                return _isExpanded;
            }
            set
            {
                SetProperty(ref _isExpanded, value);
            }
        }

        public void SelectAll(bool isChecked)
        {
            IsChecked = isChecked;
            if (Children != null)
            {
                foreach (var child in Children)
                {
                    child.SelectAll(isChecked);
                }
            }
        }

        public List<MenuDto> GetCheckedNodes()
        {
            List<MenuDto> checkedNodes = new List<MenuDto>();

            GetChildrenCheckedNotes(this, checkedNodes);

            return checkedNodes;
        }

        private void GetChildrenCheckedNotes(MenuDto node, List<MenuDto> checkedNodes)
        {
            if (node.IsChecked == true) 
            {
                checkedNodes.Add(node);
            }

            if (node.Children != null)
            {
                foreach (var child in node.Children)
                {
                    GetChildrenCheckedNotes(child, checkedNodes);
                }
            }
        }
    }
}
